﻿Public Class frmPlayTest
    Private DeckObject As Deck
    Private CardQueue As Queue(Of CardPool.Card)

    Private Sub frmPlayTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        KryptonPanelBattlefield.AllowDrop = True
        KryptonPanelGraveyard.AllowDrop = True
        'PictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this. btnImage_MouseDown);
        'this.picBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.pictureBox_DragDrop);
        'this.picBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.pictureBox_DragEnter);
    End Sub

    Public Sub LoadDeck(ByVal d As Deck)
        DeckObject = d

        Dim deckarray As New ArrayList
        For Each cs As Deck.CardSlot In DeckObject.MainDeck
            For i As Integer = 1 To cs.Quantity
                deckarray.Add(cs.Card)
            Next
        Next

        deckarray = Shuffle(deckarray)

        CardQueue = New Queue(Of CardPool.Card)
        For Each c As CardPool.Card In deckarray
            CardQueue.Enqueue(c)
        Next
    End Sub

    Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxCardInHand.MouseDown
        If e.Button = MouseButtons.Left Then
            Dim btnPic As PictureBox = CType(sender, PictureBox)
            btnPic.DoDragDrop(btnPic, DragDropEffects.Move)
        End If
    End Sub

    Private Sub KryptonPanelBattlefield_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelBattlefield.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        Dim pt As Point = KryptonPanelBattlefield.PointToClient(New Point(e.X, e.Y))

        pt.X = CInt(Math.Floor(pt.X / 90)) * 90
        pt.Y = CInt(Math.Floor(pt.Y / 120)) * 120

        pbx.Left = pt.X
        pbx.Top = pt.Y

        pbx.Dock = DockStyle.None
        KryptonPanelBattlefield.Controls.Add(pbx)
    End Sub

    Private Sub KryptonPanelGraveyard_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelGraveyard.DragDrop
        Dim pbx As PictureBox = DirectCast(e.Data.GetData(GetType(PictureBox)), PictureBox)
        pbx.Dock = DockStyle.Fill
        KryptonPanelGraveyard.Controls.Add(pbx)
        pbx.SendToBack()
    End Sub

    Private Sub KryptonPanelGraveyard_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelGraveyard.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub KryptonPanelBattlefield_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles KryptonPanelBattlefield.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub


    Private Sub PictureBoxDeck_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBoxDeck.DoubleClick
        DrawCards(1)
    End Sub


    Public Function Shuffle(ByVal Array As ArrayList) As ArrayList  'Returns Array 
        Dim intUBound As Integer
        Dim intHUBound As Integer
        Dim intRanNum As Integer
        Dim a As Object
        Dim b As Object
        Dim i As Integer


        'Starts the randon deck Shuffle
        intUBound = Array.Count - 1
        intHUBound = intUBound

        ReDim a(intUBound)
        ReDim b(intUBound)

        'On Error GoTo e

        For i = 0 To intUBound
            a(i) = Array(i) ' Sets array (a) list to (1 to cntnum)

        Next i

        Do

            Randomize() ' Activates the Random Number Generator
            intRanNum = Int(Rnd() * intUBound) ' Picks a Random number between 0 and max number
            b(0) = a(intRanNum)

            Try
                For i = 1 To intUBound ' Starts a loop
                    If intRanNum = intUBound Then intRanNum = -1 ' If the Random number = max number then the Random number becomes 0
                    intRanNum += 1  ' Adds 1 to the Random number
                    b(i) = a(intRanNum) ' b(current loop value) = The current Random number

                Next i ' Adds 1 to i and loops until i is greater than the max Number

                For i = 0 To intUBound
                    a(i) = b(i) ' Makes array a the same as array b
                Next i

                intUBound -= 1 ' Subtracts 1 from the max number
            Catch
                'If any error has occured the user is warned
                MsgBox("An Error has occured, Outcome may not be acurate", MsgBoxStyle.Critical, "Error")
            End Try
        Loop Until intUBound = -1

        Dim result As New ArrayList
        For i2 As Integer = 0 To Array.Count - 1
            result.Add(a(i2))
        Next
        Return result
    End Function

    Private Sub Draw7CardsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Draw7CardsToolStripMenuItem.Click
        DrawCards(7)
    End Sub

    Private Sub DrawCards(ByVal Count As Integer)
        'draw a card
        For i As Integer = 1 To Count
            Dim NewPictureBox As New PictureBox
            NewPictureBox.Image = PictureBoxCardInHand.Image
            Dim CardInfo As CardPool.Card = CType(CardQueue.Dequeue, CardPool.Card)
            CardPool.SetCardImage(NewPictureBox, CardInfo.MultiverseId)
            NewPictureBox.Dock = DockStyle.Left
            NewPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
            NewPictureBox.Width = PictureBoxCardInHand.Width
            NewPictureBox.Height = PictureBoxCardInHand.Height

            AddHandler NewPictureBox.MouseDown, AddressOf PictureBox2_MouseDown

            KryptonGroupBoxHand.Panel.Controls.Add(NewPictureBox)
            NewPictureBox.ContextMenuStrip = ContextMenuStripCard
        Next
    End Sub

    Private Sub DrawACardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawACardToolStripMenuItem.Click
        DrawCards(1)
    End Sub

    Private Sub ShuffleLibraryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShuffleLibraryToolStripMenuItem.Click
        'shu()
    End Sub

    Private Sub LibraryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LibraryToolStripMenuItem.Click
        Dim dlg As New dlgViewDeck

        For i As Integer = 0 To CardQueue.Count - 1
            dlg.KryptonListBoxDeck.Items.Add(CardQueue(i).Name)
        Next
        dlg.ShowDialog(Me)
    End Sub

    Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        While KryptonGroupBoxHand.Panel.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonGroupBoxHand.Panel.Controls(0), PictureBox)
            KryptonGroupBoxHand.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        While KryptonPanelBattlefield.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelBattlefield.Controls(0), PictureBox)
            KryptonPanelBattlefield.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        While KryptonPanelGraveyard.Controls.Count > 0
            Dim AsPictureBox As PictureBox = DirectCast(KryptonPanelGraveyard.Controls(0), PictureBox)
            KryptonPanelGraveyard.Controls.Remove(AsPictureBox)
            AsPictureBox.Dispose()
        End While
        LoadDeck(DeckObject)
    End Sub

    Private Sub TapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TapToolStripMenuItem.Click
        Dim pbx As PictureBox = DirectCast(DirectCast(sender, System.Windows.Forms.ToolStripMenuItem).Owner, System.Windows.Forms.ContextMenuStrip).SourceControl

        If pbx.Width = 103 Then
            pbx.Width = 75
            pbx.Height = 103
            pbx.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
        Else
            pbx.Width = 103
            pbx.Height = 75
            pbx.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
        End If
        pbx.Refresh()
    End Sub
End Class